package org.springblade.modules.client;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.library.entity.CarouselImgEntity;
import org.springblade.modules.library.service.ICarouselImgService;
import org.springblade.modules.log.entity.IntegralLogEntity;
import org.springblade.modules.log.service.IIntegralLogService;
import org.springblade.modules.log.vo.IntegralLogVO;
import org.springblade.modules.log.vo.IntegralViewVO;
import org.springblade.modules.log.wrapper.IntegralLogWrapper;
import org.springblade.modules.material.entity.TrainArchiveEntity;
import org.springblade.modules.material.service.ITrainArchiveService;
import org.springblade.modules.material.wrapper.TrainArchiveWrapper;
import org.springblade.modules.personal.entity.UserBaseEntity;
import org.springblade.modules.personal.service.IUserBaseService;
import org.springblade.modules.personal.vo.UserBaseVO;
import org.springblade.modules.personal.wrapper.UserBaseWrapper;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 客户端基本信息接口
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-client")
@Api(value = "客户端相关接口", tags = "客户端相关接口")
public class ClientController {

	private final ICarouselImgService carouselImgService;
	private final IIntegralLogService integralLogService;
	private final IUserBaseService userBaseService;
	private final ITrainArchiveService trainArchiveService;
	private final IUserService userService;

	/**
	 * 轮播图列表 分页
	 */
	@GetMapping("/carousel/list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "轮播图列表", notes = "")
	public R carouselList() {
		return R.data(carouselImgService.list(Wrappers.<CarouselImgEntity>query().lambda().eq(CarouselImgEntity::getShow,1)));
	}

	/**
	 * 个人积分列表 分页
	 * 并且可根据不同检索条件获取总的积分、学时
	 */
	@GetMapping("/myself/integral/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "个人积分列表分页", notes = "")
	public R myselfIntegralList(IntegralLogEntity  entity, Query query) {
		LambdaQueryWrapper<IntegralLogEntity> logEntityLambdaQueryWrapper=new LambdaQueryWrapper<>();
		logEntityLambdaQueryWrapper.eq(IntegralLogEntity::getUserId,AuthUtil.getUserId());
		if (null!=entity.getIntegralOrigin()) logEntityLambdaQueryWrapper.like(IntegralLogEntity::getIntegralOrigin,entity.getIntegralOrigin());
		if (null!=entity.getProjectOrigin()) logEntityLambdaQueryWrapper.like(IntegralLogEntity::getProjectOrigin,entity.getProjectOrigin());
		if (null!=entity.getRecordDate()) logEntityLambdaQueryWrapper.like(IntegralLogEntity::getRecordDate,entity.getRecordDate());
		if (null!=entity.getCreateTime())logEntityLambdaQueryWrapper.gt(IntegralLogEntity::getCreateTime,entity.getCreateTime());
		if (null!=entity.getYear())logEntityLambdaQueryWrapper.eq(IntegralLogEntity::getYear,entity.getYear());
		IPage<IntegralLogEntity> page = integralLogService.page(Condition.getPage(query), logEntityLambdaQueryWrapper);

		List<IntegralLogEntity> list = integralLogService.list(logEntityLambdaQueryWrapper);
		Map<String,Object> resultMap=new HashMap<>();
		resultMap.put("integralTotal",list.stream().mapToDouble(IntegralLogEntity::getIntegral).sum()    );
		resultMap.put("periodTotal",list.stream().mapToDouble(IntegralLogEntity::getPeriod).sum()   );
		resultMap.put("page",page);
		return R.data(resultMap);
	}


	@GetMapping("/myself/info")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "用户个人信息", notes = "")
	public R myselfInfo() {
		Long userId = AuthUtil.getUserId();
		UserBaseEntity userBaseEntity = userBaseService.selectByUserId(userId);
		UserBaseVO userBaseVO = UserBaseWrapper.build().entityVO(userBaseEntity);

		User user = userService.getById(userId);
		if (null!=user && null!=user.getAvatar())userBaseVO.setAvatar(user.getAvatar());

		return R.data(userBaseVO);
	}

	@GetMapping("/myself/trainArchive")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "我的线下实训分页列表", notes = "")
	public R myselfTrainArchive(TrainArchiveEntity trainArchive, Query query) {
		IPage<TrainArchiveEntity> pages = trainArchiveService.page(Condition.getPage(query), Condition.getQueryWrapper(trainArchive));
		return R.data(TrainArchiveWrapper.build().pageVO(pages));
	}



}
